package com.yuanfeng.commoms.rocketmq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.stereotype.Component;

/**
 * @author hyq
 * description 有序消息 生产者
 */
@Component
@Slf4j
public class MqOrderlyProducer extends MqProducer {

    public String getProducerGroup(String grpop) {
        return grpop;
    }

    /**
     * 选择队列 发送消息
     *
     * @param message 消息
     * @param id 编号
     * @return 发送结果
     */
    public SendResult sendQueueSelectorMsg(Message message, Integer id) {
        try {
            SendResult sendResult = getProducer().send(message, (queueLists, msg, o) -> {
                Integer num = (Integer) o;
                //消息队列(0-3)，默认topic下queue数量是4
                Integer queueNum = num % queueLists.size();
                return queueLists.get(queueNum);
            }, id);
            log.info("MQ消息发送成功!");
            return sendResult;
        } catch (Exception e) {
            throw new RuntimeException("MQ消息发送失败!", e);
        }
    }
}
